//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

using GuidanceExplorer.Model;

namespace GuidanceExplorer.Controls
{
    public partial class FieldSearchEntry : UserControl
    {
        private Guidance _guidance;

        private FieldSearchEntry()
        {
            InitializeComponent();
            cmbOperator.Items.Add(SearchTerms.Equals.ToString().Replace("_", " "));
            cmbOperator.Items.Add(SearchTerms.Not_Equals.ToString().Replace("_", " "));
            cmbOperator.Items.Add(SearchTerms.Starts_With.ToString().Replace("_", " "));
            cmbOperator.Items.Add(SearchTerms.Ends_With.ToString().Replace("_", " "));
            cmbOperator.Items.Add(SearchTerms.Contains.ToString().Replace("_", " "));
            cmbOperator.Items.Add(SearchTerms.Not_Contains.ToString().Replace("_", " "));

            this.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
        }

        public FieldSearchEntry(Guidance guidance) : this()
        {
            _guidance = guidance;

            cmbField.DisplayMember = "Caption";

            SortedList<string, Field> fields = new SortedList<string, Field>();
            foreach (GuidanceType type in GuidanceType.AllGuidanceTypes)
            {
                foreach (Field field in type.Fields.Values)
                {
                    if (!fields.ContainsKey(field.Caption))
                    {
                        fields.Add(field.Caption, field);
                    }
                }
            }

            foreach (Field f in fields.Values)
            {
                cmbField.Items.Add(f);
            }

            cmbField.SelectedIndex = 0;
            cmbOperator.SelectedIndex = 0;
        }

        public FieldSearchEntry(FieldSearchCriteria criteria, Guidance guidance)
            : this(guidance)
        {
            cmbValues.Text = criteria.Value;
            cmbField.SelectedIndex = GetFieldIndex(criteria.Field);
            cmbOperator.SelectedIndex = cmbOperator.Items.IndexOf(criteria.SearchTerm.ToString().Replace("_", " "));
        }

        private int GetFieldIndex(Field field)
        {
            for (int i = 0; i < cmbField.Items.Count; i++)
            {
                Field currentField = (Field)cmbField.Items[i];
                if (string.Compare(currentField.Name, field.Name) == 0)
                {
                    return i;
                }
            }

            return -1;
        }

        private void SortCombo(ComboBox combo)
        {
            SortedList<string, string> items = new SortedList<string, string>(combo.Items.Count);
            foreach (string item in combo.Items)
            {
                items.Add(item, item);
            }

            combo.Items.Clear();
            foreach (string item in items.Values)
            {
                combo.Items.Add(item);
            }
        }

        public Criteria Criteria
        {
            get
            {
                return new FieldSearchCriteria((Field)cmbField.SelectedItem, cmbValues.Text, 
                    (SearchTerms)Enum.Parse(typeof(SearchTerms), cmbOperator.SelectedItem.ToString().Replace(" ", "_")), _guidance);
            }
        }

        private void cmbField_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbField.SelectedIndex >= 0)
            {
                Field selectedField = (Field)cmbField.SelectedItem;
                List<string> values = new List<string>();

                foreach (GuidanceItem item in _guidance.Items)
                {
                    string value = item[selectedField.Name];

                    if (value == null)
                    {
                        PropertyDescriptorCollection propertyDescriptors = TypeDescriptor.GetProperties(item.GetType());

                        //Find the property in the type.
                        if (propertyDescriptors[selectedField.Name] != null)
                        {
                            value = (string)propertyDescriptors[selectedField.Name].GetValue(item);
                        }
                    }

                    if (value != null && !values.Contains(value))
                    {
                        values.Add(value);
                    }
                }

                values.Sort();

                cmbValues.Items.Clear();
                cmbValues.Items.AddRange(values.ToArray());
            }
        }
    }
}
